using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using IGG.POCO;

namespace IGGCore
{
    /// <summary>
    /// A MLB Team
    /// </summary>
    public class Team
    {
        private CultureInfo m_enCulture = new CultureInfo("en-ca");

        private string m_id;
        private string m_name;
        private string m_division;
        private string m_abbrv;
        private string m_code;
        private string m_fileCode;
        private string m_city;
        private string m_league;
        private string m_sport;
        private string m_gamesBack;
        private string m_gamesBackWildcard;
        private int m_wins;
        private int m_losses;
        private string m_magicNumber;
        private string m_lastGame;
        private string m_lastTen;
        private string m_streak;
        private TeamIdentification m_teamId;

        private Dictionary<string, Player> m_players;
        private Player[] m_lineup;
        private Player[] m_defense;

        public TeamIdentification TeamID
        {
            get
            {
                if (m_teamId == null)
                {
                    m_teamId = TeamIdentification.GetTeam(this.Name);
                }
                if (m_teamId == null)
                {
                    m_teamId = new TeamIdentification("", "");
                    m_teamId.TimeZoneDiff = 1;
                    m_teamId.Abbr = "";
                    m_teamId.CNNSIPage = "http://sportsillustrated.cnn.com/baseball/mlb/rosters/chicago-cubs/byLAST_NM.html";
                    m_teamId.YahooPage = "http://ca.sports.yahoo.com/mlb/teams/chc/photos";
                    m_teamId.BackColor = new int[] { 255, 255, 255 };
                    m_teamId.ForeColor = new int[] { 0, 51, 102 };
                    m_teamId.BackColorAway = new int[] { 196, 194, 194 };
                    m_teamId.ForeColorAway = new int[] { 0, 51, 102 };
                }
                return m_teamId;
            }
        }

        public string RssFeed { get; set; }
        public string AbbrName { get; set; }
        public string AltCode { get; set; }
        public string DepthChartSource { get; set; }
        public DepthChart DepthChart { get; set; }
        public List<News> News { get; set; }
        
        public List<string> Facts { get; set; }
        public List<string> BeatWriters { get; set; }
        public Tweet[] BeatTweets { get; set; }

        public string MagicNumber
        {
            get { return m_magicNumber; }
            set { m_magicNumber = value; }
        }

        public string LastTen
        {
            get { return m_lastTen; }
            set { m_lastTen = value; }
        }

        public string Streak
        {
            get { return m_streak; }
            set { m_streak = value; }
        }

        public string LastGame
        {
            get { return m_lastGame; }
            set { m_lastGame = value; }
        }


        public Team()
        {
            m_players = new Dictionary<string, Player>();
            m_lineup = new Player[10];
            m_defense = new Player[9];
        }

        public Player[] Lineup
        {
            get { return m_lineup; }
            set { m_lineup = value; }
        }

        public Player[] Defense
        {
            get { return m_defense; }
            set { m_defense = value; }
        }

        public Dictionary<string, Player> Players
        {
            get { return m_players; }
            set { m_players = value; }
        }

        public int Wins
        {
            get { return m_wins; }
            set { m_wins = value; }
        }
        
        public int Losses
        {
            get { return m_losses; }
            set { m_losses = value; }
        }

        public string Percent { get; set; }

        public float WinPercent
        {
            get
            {
                if ((m_wins + m_losses) > 0)
                {
                    return ((float)m_wins) / ((float)(m_wins + m_losses));
                }
                else
                {
                    return 0;
                }
            }
        }

        public string Code
        {
            get { return m_code; }
            set { m_code = value; }
        }

        public string FileCode
        {
            get { return m_fileCode; }
            set { m_fileCode = value; }
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }
        
        public string Abbrv
        {
            get { return m_abbrv; }
            set { m_abbrv = value; }
        }

        public string City
        {
            get { return m_city; }
            set { m_city = value; }
        }

        public string League
        {
            get { return m_league; }
            set { m_league = value; }
        }

        public string Sport
        {
            get { return m_sport; }
            set { m_sport = value; }
        }

        public string Division
        {
            get { return m_division; }
            set { m_division = value; }
        }

        public string Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public string GamesBack
        {
            get { return m_gamesBack; }
            set { m_gamesBack = value; }
        }

        public string GamesBackWildcard
        {
            get { return m_gamesBackWildcard; }
            set { m_gamesBackWildcard = value; }
        }

        public float GetNumericGamesBack()
        {
            if (m_gamesBack == null || m_gamesBack.Equals("-") || m_gamesBack.Equals(""))
            {
                return 0;
            }
            else
            {
                return float.Parse(m_gamesBack, new CultureInfo("EN-CA") );
            }
        }

        public float GetNumericWildCardGamesBack()
        {
            if (m_gamesBackWildcard == null || m_gamesBackWildcard.Equals("-") || m_gamesBackWildcard.Equals(""))
            {
                return 0;
            }
            else
            {
                return float.Parse(m_gamesBackWildcard, m_enCulture);
            }
        }
    }
}
